rm(list = ls())

library(causalsens)
library(DirectEffects)
library(haven)
library(foreign)
library(rio)
library(tidyverse)
library(sjlabelled)
library(memisc)
library(expss)
library(dplyr)
library(tidyverse)
library(magrittr)
library(sandwich)
library(miceadds)
library(stargazer)

cw <- import("/Users/christopherblair/Desktop/JOP Replication/ams175_R.dta")

core <- c("age", "rankgrade", "school", "monthsoversea")
cw[core] <- lapply(cw[core], factor)

#BLACKWELL (2014) SENSITIVITY TESTS

#CW USE
use1 <- lm(use ~ numchamber+ nummask+ age2+age3+age4+age5+age6+age7+age8+age9+age10+age11+age12+
             school2+school3+school4+school5+school6+rankgrade2+rankgrade3+rankgrade4+rankgrade5+
             rankgrade6+monthsoversea2+monthsoversea3+monthsoversea4+monthsoversea5+monthsoversea6+
             monthsoversea7+monthsoversea8+monthsoversea9+monthsoversea10+ally_relations+
             orientation_officers+infocenter+orient_meet+war_interest+honolulu_contact+base1+base2+base3+base4+base5, data=cw)
summary(use1, cluster="base") 

use1_t <- glm(numchamber ~ training +infocenter + orient_meet, data=cw)
summary(use1_t, cluster="base") 

alpha <- seq(-.07, .07, by = .005)
use_sens <- causalsens(use1, use1_t, ~ age2+age3+age4+age5+age6+age7+age8+age9+age10+age11+age12+
  school2+school3+school4+school5+school6+rankgrade2+rankgrade3+rankgrade4+rankgrade5+
  rankgrade6+monthsoversea2+monthsoversea3+monthsoversea4+monthsoversea5+monthsoversea6+
  monthsoversea7+monthsoversea8+monthsoversea9+monthsoversea10, data = cw, alpha = alpha, 
  confound = one.sided.att)

png(file="/Users/christopherblair/Desktop/JOP Replication/Results/causalsens1.png", width=6, height=4, units="in", res=1200)
plot(use_sens,  type = "raw", bty = "l", ylab="Effect of Gas Exposure", xlab="Amount of Counfounding", main="Support for Using Chemical Weapons Against Japan")
dev.off()

png(file="/Users/christopherblair/Desktop/JOP Replication/Results/causalsens2.png", width=6, height=4, units="in", res=1200)
plot(use_sens,  type = "r.squared", bty = "l", ylab="Effect of Gas Exposure", xlab="Variance Explained by Counfounding", main="Support for Using Chemical Weapons Against Japan")
dev.off()

